
package com.iceman.secretsms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class SecretSmsActivity extends Activity {
    private EditText name, number;

    private Button add;

    private RmsAdapter rms;

    private ListView mUserList;

    private User[] users;
    
    private UserListAdapter mUserAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (StaticData.sNotificationMessage != null) {
            StaticData.sNotificationMessage.cancleNotification();
        }
        setContentView(R.layout.main);
        name = (EditText) findViewById(R.id.name);
        number = (EditText) findViewById(R.id.number);
        add = (Button) findViewById(R.id.add);
        mUserList = (ListView) findViewById(R.id.userlist);
        rms = RmsAdapter.getInstance(this);
        getUsers();
        mUserAdapter = new UserListAdapter();
        mUserList.setAdapter(mUserAdapter);
        mUserList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent it = new Intent(SecretSmsActivity.this, SmsActivity.class);
                it.putExtra("number", users[position].getNumber());
                startActivity(it);
            }
        });
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rms.putUser(new User(number.getEditableText().toString(), name.getEditableText()
                        .toString()));
                rms.close();
                getUsers();
                mUserAdapter.notifyDataSetChanged();
            }
        });

    }

    private void getUsers() {
        users = rms.getUser();
    }

    class UserListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (users == null) {
                return 0;
            } else {
                return users.length;
            }
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView = getLayoutInflater().inflate(R.layout.userlist_item_layout, null);
            }
            TextView user = (TextView) convertView.findViewById(R.id.user_text);
            user.setText(users[position].toString());
            return convertView;
        }

    }
}
